#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
##  用代理获取游客的cookie 并插入redis队列中  ###
@File        :   cookie_pool.py
@Contact     :   18574945291@163.com
@License     :   (C)Copyright 2018-20xx, Liugroup-NLPR-CASIA
@Modify Time :   2020/12/11 9:38
@Author      :   code_wizard 1978992154@qq.com
"""


import requests
import sys, os
import redis
import umsgpack
import random
from fake_useragent import UserAgent
sys.path.append(os.getcwd())
import local_settings

INDEX_URL = "https://www.qcc.com/"


class QccCookie(object):
    def __init__(self):
        self.redis_connect = redis.Redis(**local_settings.redis_dev)
        self.headers = {
            "Host": "www.qcc.com",
            "Connection": "keep-alive",
            "Cache-Control": "max-age=0",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": None,
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh,en;q=0.9,zh-CN;q=0.8"
        }

    def cookie_init(self, user_agent):
        if user_agent is None:
            return None
        self.headers["User-Agent"] = user_agent
        response = requests.get(INDEX_URL, headers=self.headers, proxies=local_settings.PROXIES)
        cookie_dict = dict()
        cookie_list = list()
        if response.status_code == 200:
            for cookie in response.cookies:
                cookie_dict[cookie.name] = cookie.value
            for i, j in cookie_dict.items():
                cookie_list.append(i + "=" + j)
            return ";".join(cookie_list)
        else:
            print("代理请求企查查失败")
            return None

    def set_cookie_pool(self):
        rand_agent = str(random.choice(UserAgent().data_browsers.get("chrome")))
        cookie_str = self.cookie_init(rand_agent)
        if cookie_str:
            temp_dict = dict()
            temp_dict['agent'] = rand_agent
            temp_dict['cookie'] = cookie_str
            self.redis_connect.lpush(local_settings.COOKIE_POOL_NAME, umsgpack.packb(temp_dict))
            print("cookie入队成功")
        else:
            print("获取cookie失败")

    def get_cookie_pool(self):
        """
        从队列中获取cookie和user-agent {'agent': '', 'cookie': ''}
        """
        res = self.redis_connect.rpop(local_settings.COOKIE_POOL_NAME)
        if res is None:
            print(local_settings.COOKIE_POOL_NAME+" cookie pool is null ")
        else:
            return umsgpack.unpackb(res)

    def __del__(self):
        """ 释放相关资源 """
        self.redis_connect.close()


if __name__ == '__main__':
    qcc = QccCookie()
    while True:
        try:
            qcc.set_cookie_pool()
        except Exception as e:
            print(e)
    # print(qcc.get_cookie_pool())
